//
//  OrderTimeLineCell.m
//  danpin
//
//  Created by chuangjia on 3/11/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "OrderTimeLineCell.h"
#import "TTTAttributedLabel.h"
#import "CalcLabel.h"
#import "EXPhotoViewer.h"
@interface OrderTimeLineCell ()<TTTAttributedLabelDelegate>
{
    
}
@property(nonatomic,strong)UILabel *lbSegment;
@property(nonatomic,strong) UILabel *lbTime;  //时间
@property(nonatomic,strong) TTTAttributedLabel *lbMessage;  //日期
@property(nonatomic,strong) UILabel *lbDate;  //日期

@property(nonatomic,strong)UIScrollView *imageScroll;

@end
@implementation OrderTimeLineCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
        
        _lbSegment =[[UILabel alloc] initWithFrame:CGRectMake(0,0, 1,self.bounds.size.height)];
        _lbSegment.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_lbSegment];

        _lbTime =[[UILabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, PHOTO_FRAME_WIDTH,PHOTO_FRAME_WIDTH)];
        _lbTime.backgroundColor = [UIColor lightGrayColor];
        _lbTime.textColor = [UIColor whiteColor];
        _lbTime.layer.cornerRadius = _lbTime.frame.size.width / 2;
        [_lbTime.layer setMasksToBounds:YES];
        _lbTime.textAlignment = NSTextAlignmentLeft;
        _lbTime.font = FONT(12);
        [self.contentView addSubview:_lbTime];
        
        _lbMessage =[[TTTAttributedLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH*3,PHOTO_FRAME_WIDTH, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*6,40)];
        _lbMessage.backgroundColor =  [UIColor clearColor];
        _lbMessage.textColor = CONTEXT_COLOR;//UIColorFromRGB(0x808080);
        _lbMessage.textAlignment = NSTextAlignmentLeft;
        _lbMessage.lineBreakMode = NSLineBreakByWordWrapping|NSLineBreakByTruncatingTail;
        _lbMessage.numberOfLines=0;
        _lbMessage.font = FONT(12);
        _lbMessage.delegate =self;
        [self.contentView addSubview:_lbMessage];
        //
        _imageScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH*3, 0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*6, PHOTO_FRAME_WIDTH*6)];
        _imageScroll.pagingEnabled = YES;
        _imageScroll.showsHorizontalScrollIndicator = NO;
        _imageScroll.showsVerticalScrollIndicator = NO;
        _imageScroll.scrollsToTop = NO;
        [self.contentView addSubview:_imageScroll];
        //
        _lbDate =[[UILabel alloc] initWithFrame:CGRectZero];
        _lbDate.backgroundColor = [UIColor clearColor];
        _lbDate.textColor = CONTEXT_COLOR;
        _lbDate.textAlignment = NSTextAlignmentLeft;
        _lbDate.font = FONT(12);
        [self.contentView addSubview:_lbDate];
    }
    return self;
}
-(void)dealloc
{
    self.actionBlock =nil;
    self.data=nil;
    self.indexPath=nil;
    self.other=nil;
    self.key=nil;
    
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
   
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    if([self.key intValue]==0)
    {
        self.lbSegment.frame=CGRectMake(PHOTO_FRAME_WIDTH*1.5, PHOTO_FRAME_WIDTH, 1, self.bounds.size.height-PHOTO_FRAME_WIDTH);
        self.lbTime.frame=CGRectMake(PHOTO_FRAME_WIDTH*1.5-PHOTO_FRAME_WIDTH/2, PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH);
        self.lbTime.backgroundColor = [UIColor orangeColor];
    }else if ([self.key intValue]==1)
    {
        self.lbSegment.frame=CGRectMake(PHOTO_FRAME_WIDTH*1.5, 0, 1, self.bounds.size.height/2);
        self.lbTime.frame=CGRectMake(PHOTO_FRAME_WIDTH*1.5-PHOTO_FRAME_WIDTH/2, self.bounds.size.height/2-PHOTO_FRAME_WIDTH/2, PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH);
        self.lbTime.backgroundColor = [UIColor lightGrayColor];
    }else{
        self.lbSegment.frame=CGRectMake(PHOTO_FRAME_WIDTH*1.5, 0, 1, self.bounds.size.height);
        self.lbTime.frame=CGRectMake(PHOTO_FRAME_WIDTH*1.5-PHOTO_FRAME_WIDTH/2, PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH);
        self.lbTime.backgroundColor = [UIColor lightGrayColor];
    }
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.key =key;
    self.indexPath=indexPath;
    NSDictionary *dict =(NSDictionary*)model;
    self.data =model;
    //
    NSDictionary *flow_info =[dict dictionaryObjectForKey:@"flow_info"];
    if (flow_info==nil || ![flow_info isKindOfClass:[NSDictionary class]]) {
        return;
    }
    float fheight = 0;
    NSString *title =[flow_info stringForKey:@"content" withDefaultValue:@""];
    NSString *express_no =[flow_info stringForKey:@"gaoliang" withDefaultValue:@""];
    NSString *link =[flow_info stringForKey:@"link" withDefaultValue:@""];
   
    if (title.length>0) {
        [self.lbMessage setText:title afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString  *(NSMutableAttributedString *attributedString)
         {
             [attributedString addAttribute:NSFontAttributeName
                                      value:FONT(12)
                                      range:NSMakeRange(0,[attributedString length])];
             CGFloat lineSpace = 2.0f;
             CTParagraphStyleSetting lineSpaceStyle;
             lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;
             lineSpaceStyle.valueSize = sizeof(lineSpace);
             lineSpaceStyle.value =&lineSpace;
             
             //设置文本段间距
             CGFloat paragraphSpacing = 0.0f;
             CTParagraphStyleSetting paragraphSpaceStyle;
             paragraphSpaceStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
             paragraphSpaceStyle.valueSize = sizeof(CGFloat);
             paragraphSpaceStyle.value = &paragraphSpacing;
             
             //创建设置数组
             CTParagraphStyleSetting settings[ ] ={lineSpaceStyle,paragraphSpaceStyle};
             CTParagraphStyleRef style = CTParagraphStyleCreate(settings ,2);
             
             //给文本添加设置
             [attributedString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)CFBridgingRelease(style) range:NSMakeRange(0 , [attributedString length])];
             return attributedString;
             
         }];
         fheight = [CalcLabel getAttributedStringHeightWidthValue:(DEVICE_WIDTH- PHOTO_FRAME_WIDTH*4) str:self.lbMessage.attributedText];
         self.lbMessage.frame =CGRectMake(PHOTO_FRAME_WIDTH*3, PHOTO_FRAME_WIDTH, DEVICE_WIDTH - PHOTO_FRAME_WIDTH*4, fheight);
        fheight =PHOTO_FRAME_WIDTH*2+fheight;
        //
        NSRange range = [title rangeOfString:express_no];

        if (range.location != NSNotFound) {
            if (link.length==0) {
                link = @"http://danpin.com";
            }
            //设置字体颜色
            [self.lbMessage addLinkToURL:[NSURL URLWithString:link] withRange:range];
       }else{
            NSLog(@"Not Found");
        }
    }
    NSArray *image = [flow_info arrayObjectForKey:@"image"];
    if (image && [image isKindOfClass:[NSArray class]]&&[image count]>0) {
        NSArray *arr = (NSArray *)image;
        if (arr.count>0) {
            _imageScroll.frame =CGRectMake(PHOTO_FRAME_WIDTH*3,fheight, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4, PHOTO_FRAME_WIDTH*6);
            _imageScroll.contentSize = CGSizeMake(PHOTO_FRAME_WIDTH*6 * [arr count], _imageScroll.frame.size.height);
            for (int i=0;i<[arr count];i++) {
                NSString *dict =[arr objectAtIndex:i];
               
                UIImageView *iconView =[[UIImageView alloc] init];
                iconView.frame=CGRectMake(i*PHOTO_FRAME_WIDTH*7, 0, PHOTO_FRAME_WIDTH*6, PHOTO_FRAME_WIDTH*6);
                iconView.tag =i;
                [iconView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", dict]] placeholderImage:[UIImage imageNamed:@"image.png"]];
                iconView.userInteractionEnabled = YES;
             
                UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
                btn.backgroundColor=[UIColor clearColor];
                btn.frame =iconView.frame;
                btn.tag = i;
                [btn addTarget:self action:@selector(clickEvent:) forControlEvents:UIControlEventTouchUpInside];
             
                [_imageScroll addSubview:iconView];
                iconView=nil;
                [_imageScroll addSubview:btn];
            }
            
            fheight = fheight + PHOTO_FRAME_WIDTH*8;
        }
    }else{
        _imageScroll.frame =CGRectZero;
    }
    //
    _lbDate.frame =CGRectMake(PHOTO_FRAME_WIDTH*3, fheight, DEVICE_WIDTH - PHOTO_FRAME_WIDTH*4, PHOTO_FRAME_WIDTH*2);
    _lbDate.text =[dict stringForKey:@"add_time" withDefaultValue:@""];
    [self layoutSubviews];
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)clickEvent:(id)sender
{
    UIButton *btn =(UIButton*)sender;
    int index =(int) btn.tag;
    //
    NSDictionary *flow_info =[self.data dictionaryObjectForKey:@"flow_info"];
    if (flow_info==nil || ![flow_info isKindOfClass:[NSDictionary class]]) {
        return;
    }
    NSArray *image = [flow_info arrayObjectForKey:@"image"];
    if (image && [image isKindOfClass:[NSArray class]]&&[image count]>0)
    {
        NSString *dict = [image objectAtIndex:index];
        if (dict.length>0) {
            UIImageView *iconView =[[UIImageView alloc] initWithFrame:CGRectMake(DEVICE_WIDTH/2,  DEVICE_HEIGHT/2, DEVICE_WIDTH/2, DEVICE_HEIGHT/2)];
            [iconView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", dict]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                [EXPhotoViewer showImageFrom:iconView];
            }];
            
        }
        
    }
    
}

- (void)attributedLabel:(__unused TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
{
    NSDictionary *flow_info =[self.data dictionaryObjectForKey:@"flow_info"];
    if (flow_info==nil || ![flow_info isKindOfClass:[NSDictionary class]]) {
        return;
    }
    NSString *link=[flow_info stringForKey:@"link" withDefaultValue:@""];
    int tariff =[[flow_info numberStringForKey:@"tariff"] intValue];
    int id_card =[[flow_info numberStringForKey:@"id_card"] intValue];
    if (tariff==1 && id_card==0) {
        if (self.actionBlock) {
            self.actionBlock(BEventType_tariff,nil,self.data,nil,self.indexPath);
        }
    }else if (tariff==0 && id_card==1) {
        if (self.actionBlock) {
            self.actionBlock(BEventType_card,nil,self.data,nil,self.indexPath);
        }
    }else{
        if (self.actionBlock) {
            self.actionBlock(BEventType_link,nil,self.data,link,self.indexPath);
        }
    }

}

-(void)clickHelpEvent
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_help,nil,self.data,nil,self.indexPath);
    }
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    CGFloat fheight = 0;
    NSDictionary *dict =(NSDictionary*)model;
    NSDictionary *flow_info =[dict dictionaryObjectForKey:@"flow_info"];
    if (flow_info&&[flow_info isKindOfClass:[NSDictionary class]]) {
        NSString *title =[flow_info stringForKey:@"content" withDefaultValue:@""];
        if(title.length>0)
        {
            NSMutableAttributedString* attributedString =[[NSMutableAttributedString alloc]initWithString:title];
            
            [attributedString addAttribute:NSFontAttributeName
                                     value:FONT(12)
                                     range:NSMakeRange(0,[attributedString length])];
            //设置字体颜色
            [attributedString addAttribute:(id)kCTForegroundColorAttributeName value:CONTEXT_COLOR range:NSMakeRange(0,[attributedString length])];
            
            CGFloat lineSpace = 2.0f;
            CTParagraphStyleSetting lineSpaceStyle;
            lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;
            lineSpaceStyle.valueSize = sizeof(lineSpace);
            lineSpaceStyle.value =&lineSpace;
            
            //设置文本段间距
            CGFloat paragraphSpacing = 0.0f;
            CTParagraphStyleSetting paragraphSpaceStyle;
            paragraphSpaceStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
            paragraphSpaceStyle.valueSize = sizeof(CGFloat);
            paragraphSpaceStyle.value = &paragraphSpacing;
            
            //创建设置数组
            CTParagraphStyleSetting settings[ ] ={lineSpaceStyle,paragraphSpaceStyle};
            CTParagraphStyleRef style = CTParagraphStyleCreate(settings ,2);
            
            //给文本添加设置
            [attributedString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)CFBridgingRelease(style) range:NSMakeRange(0 , [attributedString length])];
            
            fheight = [CalcLabel getAttributedStringHeightWidthValue:(DEVICE_WIDTH- PHOTO_FRAME_WIDTH*4) str:attributedString];
            attributedString =nil;
            ;
        }
    }
  
    NSArray *image = [flow_info arrayObjectForKey:@"image"];
    if (image && [image isKindOfClass:[NSArray class]]) {
        NSArray *arr = (NSArray *)image;
        if (arr.count>0) {
            fheight = fheight + PHOTO_FRAME_WIDTH*8;
        }
    }
    return fheight + PHOTO_FRAME_WIDTH*5;
}


@end